package com.jiyun.controller;


import com.cn.jiyun.entity.pojo.Order;
import com.jiyun.entity.vo.OrderVo;
import com.jiyun.result.Result;
import com.jiyun.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/order")
public class    OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("/findAll")
    public Result<List<Order>> findAll(){
        return Result.ok(orderService.findAll());
    }
    @RequestMapping("/findById")
    public Result<Order> findById(@RequestParam Integer id){
        return Result.ok(orderService.findById(id));
    }
    @PostMapping("/addOrder")
    public Result<Boolean> addOrder(@RequestBody OrderVo orderVo){
        System.out.println("----------"+orderVo);
        Order order1 = new Order();
        order1.setUserId(orderVo.getUserId());
        order1.setOrderStatus(orderVo.getOrderStatus());
        order1.setOrderType(orderVo.getOrderType());
        order1.setTotalPrice(orderVo.getTotalPrice());
        order1.setOrderTime(new Date());
        order1.setRelatedId(orderVo.getRelatedId());
        order1.setHaveTime(orderVo.getHaveTime());
        order1.setImgUrl(orderVo.getImgUrl());
        order1.setDingdanName(orderVo.getDingdanName());
        Random random=new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 13; i++) {
            sb.append(random.nextInt(10));
        }
        order1.setOrderNo(sb.toString());
        return Result.ok(orderService.addOrder(order1));
    }
}
